package com.yongcheon.starhealthcare.facebook;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.ProfilePictureView;
import com.yongcheon.starhealthcare.R;
import com.yongcheon.starhealthcare.main.MainActivity;
import com.yongcheon.starhealthcare.main.drawer.DrawListItemFactory;

/**
 * Created by yongcheon on 2015-02-05.
 */
public class FacebookCallback implements Session.StatusCallback {
    public static Session session;

    private ProfilePictureView profilePictureView;
    private TextView profileTextView;
    private TextView faceBookTextView;
    private ImageView profileImageView;
    private MainActivity mainActivity;


    public FacebookCallback(MainActivity mainActivity){
        this.mainActivity = mainActivity;
    }

    @Override
    public void call(Session session, SessionState sessionState, Exception e) {
        setView();
        if (session != null){
            this.session = session;
            if(session.isOpened()){
                setFaceUserProfile(session);
            }else if (sessionState.isClosed()) {
                setEmpteyProfile();
            }
        }else if (sessionState.isClosed()) {
            setEmpteyProfile();
        }
    }

    private void setFaceUserProfile(final Session session){
        profileImageView.setVisibility(ViewGroup.GONE);
        profilePictureView.setVisibility(ViewGroup.VISIBLE);
        faceBookTextView.setText(mainActivity.getResources().getString(R.string.logout_facebook));
        Request request = Request.newMeRequest(session,
                new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (session == Session.getActiveSession()) {
                            if (user != null) {
                                profilePictureView.setProfileId(user.getId());
                                profileTextView.setText(user.getName());
                            }
                        }
                        if (response.getError() != null) {
                        }
                    }
                });
        request.executeAsync();
    }

    private void setEmpteyProfile(){
        profileImageView.setVisibility(ViewGroup.VISIBLE);
        profilePictureView.setVisibility(ViewGroup.GONE);
        profileTextView.setText(mainActivity.getResources().getString(R.string.please_login_facebook));
        faceBookTextView.setText(mainActivity.getResources().getString(R.string.login_facebook));
    }

    private void setView(){
        if(profileTextView == null) {
            ListView listView = (ListView) mainActivity.findViewById(R.id.left_drawer);
            profileTextView = (TextView) listView.getChildAt(DrawListItemFactory.INDEX_PROFILE).findViewById(R.id.list_text);
            profilePictureView = (ProfilePictureView) listView.getChildAt(DrawListItemFactory.INDEX_PROFILE).findViewById(R.id.face_profile_img);
            profileImageView = (ImageView) listView.getChildAt(DrawListItemFactory.INDEX_PROFILE).findViewById(R.id.list_img);
            faceBookTextView = (TextView) listView.getChildAt(DrawListItemFactory.INDEX_FACEBOOK).findViewById(R.id.list_text);
        }
    }

}
